#!/usr/bin/env python3
# Created by han on 2016/6/21
import sys,os,pickle
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from module import school,public
from conf import db_conf, log_conf

# TEACHER_FILE = db_conf.DB_BASE + "/" + "teacher.db"
# COURSE_FILE = db_conf.DB_BASE+ "/" + "course.db"

TEACHER_FILE = db_conf.SHOOL_DB_BASE + "/" + "teacher.db"
COURSE_FILE = db_conf.SHOOL_DB_BASE + "/" + "course.db"

STUDENT_DB = db_conf.STUDENT_DB_BASE


def make_teacher():
    """
    生成老师对象
    :return:
    """
    msg = "欢迎来到教导处，请创建老师"
    public.color_msg(33,msg,40)
    name = input("输入老师姓名: ")
    teacher_status = judge(name)
    if not teacher_status:
        return False
    passwd = input("老师登陆密码： ")
    age = input("输入老师年龄: ")
    sex = input("输入老师性别: ")
    interest = input("输入老师爱好: ")
    spe = input("输入老师的特长专业: ")
    teacher_obj = school.Teacher(name, passwd, age, sex, interest, spe)
    return teacher_obj


def judge(name):
    """
    判断是否有这个老师
    :param name:将名称输入
    :return:返回真或假
    """
    file_satus = os.path.exists(TEACHER_FILE)
    if not file_satus:
        return True
    teacher_obj_list = pickle.load(open(TEACHER_FILE,"rb"))
    for i in teacher_obj_list:
        if i.name == name:
            return False
    return True


def save_teacher(teacher_obj):
    """
    将老师对象存入文件
    :param:(teacher_obj,name)
    :return:
    """
    teacher_list = []
    while True:
        user_chose = input("是否保存老师:[y/n]: ")#是否存储老师信息
        if user_chose == "n":
            return False
        elif user_chose == "y":
            file_status = os.path.exists(TEACHER_FILE)
            if not file_status:#如果没有创建过老师
                teacher_list.append(teacher_obj)#把老师对象当到空列表中
                with open(TEACHER_FILE, "wb") as teacher_db:
                    pickle.dump(teacher_list, teacher_db)#保存至文件里
                return True
            else:#如果有这个文件
                with open(TEACHER_FILE, "rb") as teacher_db:
                    teacher_list = pickle.load(teacher_db)#先读取这个文件
                teacher_list.append(teacher_obj)#在这个文件中最加老师对象
                with open(TEACHER_FILE, "wb") as new_teacher_db:
                    pickle.dump(teacher_list,new_teacher_db)#再把这个文件放回去
                return True


#未完成
def del_teacher():
    """
    注销老师
    :return:
    """
    """
    注销老师
    :return:
    """
    file_status = os.path.exists(TEACHER_FILE)
    if not file_status:
        msg = "没有老师可以删除"
        public.color_msg(31,msg,40)
    else:
        teacher_list = pickle.load(open(TEACHER_FILE, "rb"))
        #删除老师时，对应学生选好的课程也要被删除。


def chose_teacher_obj():
    """
    选择已经创建好的老师对象列表
    :return:
    """
    file_status = os.path.exists(TEACHER_FILE)
    if not file_status:
        msg = "还没有老师"
        public.color_msg(31, msg, 40)
        return False
    else:
        with open(TEACHER_FILE, "rb") as teacher_db:
            teacher_list = pickle.load(teacher_db)  #取到含有老师对象的列表
        return teacher_list
#修改课程创建文件


def show_teacher(obj):
    """
    展示老师
    :param obj:老师对象列表
    :return:
    """
    num = 0
    for i in obj:
        num += 1
        msg = "{num:<5} 姓名：{name:^5}:\t年龄：{age:^5},\t性别：{sex:^5},\t爱好：{interest:>7}." \
              "\n\t\t\t\t\t\t专业特长：{speciality:*^10}\n".format(num=num,name=i.name, age=i.age, sex=i.sex,
                                              interest=i.interest,speciality=i.speciality)
        print(msg)
    return True


def chose_course_obj(db_file=COURSE_FILE):
    """
    获取所有的课程对对象列表
    :return:课程对象或者False
    """
    course_file = os.path.exists(db_file)#判断是否有这个文件
    if not course_file:#如果没有这个文件表示还没有创建课程
        msg = "还没有创建课程"
        public.color_msg(31, msg, 40)
        return False
    else:
        with open(db_file, "rb") as course_file:
            course_obj = pickle.load(course_file)#pickle反序列化出课程对象列表
        return course_obj


def show_course(obj):
    num = 0
    for i in obj:
        num += 1
        msg = "{num:<5} 课程：{name:^5},\t任课老师：{teacher:^5},\t课时费：{cost:^5},\t经验值：{exp:^5}"\
            .format(num=num,name=i.name,teacher=i.teacher.name,cost=i.cost,exp=i.exp)
        print(msg)
    return True


def course_out(func):
    """
    在创建课程前先检查是否已经创建过老师，如果没有就通过装饰器创建老师
    :param func:创建课程函数
    :return:
    """
    def inner():
        file_status = os.path.exists(TEACHER_FILE)
        if file_status:         #如果有老师的数据文件则返回到注册课程函数
            return func()
        else:       #没有老师数据文件说明没有注册老师，所以要先注册老师
            msg = "请先注册老师，再创建课程"
            public.color_msg(33, msg, 40)
            teacher_name = make_teacher()
            save_teacher(teacher_name)
            return func()
    return inner


@course_out
def make_course():
    """
    生成课程对象
    :return:
    """
    while True:
        msg = "创建课程"
        public.color_msg(32, msg, 40)
        course_list = ["math", "chinese", "english", "computer", "dance", "economics"]
        for i in enumerate(course_list):#遍历所有的课程名单
            print(i[0]+1,i[1])
        name = input("请输入课程名称： ")#输入课程名单全拼
        if name not in course_list:#如果输入的名称不在课程名单里
            msg = "输入有误请重新输入"
            public.color_msg(31, msg, 40)
            continue
        while True:
            cost = input("请输入课时费： ")
            if not cost.isdigit():#课时费必须是数字，好做加减法
                msg = "重新输入数字"
                public.color_msg(31,msg,40)
                # continue
            else:
                cost = int(cost)  # 转换成为整数类型
                break
        teacher_name = chose_teacher_obj()
        show_teacher(teacher_name)#展示出所有老师以备后面选择使用
        while True:
            teacher_num = input("请选择授课老师： ")
            with open(TEACHER_FILE,"rb") as teacher_file:
                teacher_list = pickle.load(teacher_file)#读取teacher的obj的列表
            if teacher_num.isdigit() and int(teacher_num) <= len(teacher_list):#如果 用户输入的是数字，并且数字小于整个的长度
                teacher_obj = teacher_list[int(teacher_num)-1]#找到用户输入对应的老师对象
                break
            else:
                msg = "输入有误请重新输入： "
                public.color_msg(31,msg,40)
                continue
        while True:
            exp = input("学习课程经验值： ")
            if not exp.isdigit():
                msg = "请输入数字"
                public.color_msg(31, msg, 40)
            else:
                exp = int(exp)
                break
        course_name = school.Course(name, cost, teacher_obj, exp)
        return course_name


def save_course(course_obj):
    """
    是否存储课程
    :param course_obj:
    :return:
    """
    course_list = []
    while True:
        user_chose = input("是否储存课程 [y/n]： ")
        if user_chose == "n":#不存入文件
            return False
        elif user_chose == "y":#存入文件
            course_status = os.path.exists(COURSE_FILE)#判断课程文件是否存在
            if not course_status:#如果不存在
                course_list.append(course_obj)#把课程添加到课程空列表中
                with open(COURSE_FILE, 'wb') as course_file:
                    pickle.dump(course_list, course_file)
                msg = "保存课程成功"
                public.color_msg(32,msg,40)
                return True
            else:#如果之前已经有这个文件了
                with open(COURSE_FILE, 'rb') as course_file:#读出文件中的课程
                    course_list = pickle.load(course_file)
                course_list.append(course_obj)#将学生选择的课程放入到已经存放的用户文件中
                with open(COURSE_FILE, "wb") as course_file:
                    pickle.dump(course_list, course_file)
                msg = "保存课程成功"
                public.color_msg(32, msg, 40)
                return True
        else:
            msg = "您的输入有误请重新输入"
            public.color_msg(31, msg, 40)


def go_to_school(stu_course, stu):
    # ts_before = stu_course.teacher.asset#老师上课前
    # print(ts_before)
    stu_exp = stu_course.attend()#执行课程的上课方法会返回学生的经验
    # ts_after = stu_course.teacher.asset#执行上课后老师的钱
    stu.go(stu_exp)#执行学生的上课方法，把经验传递过去
    # print(ts_after)                                    #将变动后的老师对象存放在老师的文件中
    return stu_exp, stu_course#学生的经验和课程对象


def show_stu(stu_obj):
    stu = "姓名：{name:*^10},经验：{exp:*^10}".format(name=stu_obj.name,exp=stu_obj.exp)
    public.color_msg(35, stu ,40)
